package com.dense.kuiniu.bookborrow.view.dialog;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dense.kuiniu.bookborrow.R;

/**
 * @author Administrator
 * @date 2020/8/11
 */
public class CommonDialog extends DialogMeasure {
    private TextView mTvMsg;
    private TextView mTvButton;
    private ImageView mIcon;

    public CommonDialog(Context context) {
        super(context);
    }

    @Override
    protected int layoutRes() {
        return R.layout.dialog_common;
    }

    @Override
    protected void init(Context context, View view) {
        mTvButton = view.findViewById(R.id.tv_exit);
        mTvButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exit();
            }
        });
      //  setCanceledOnTouchOutside(false);
        mTvMsg = view.findViewById(R.id.tv_msg);
        mIcon = view.findViewById(R.id.iv_icon);
    }

    public CommonDialog confirm(View.OnClickListener listener){
        mTvButton.setOnClickListener(listener);
        return this;
    }


    public CommonDialog loading(String msg) {
        if (mTvMsg != null && mTvButton != null && mIcon != null) {
            if (!msg.isEmpty()) {
                mTvMsg.setText(msg);
            }
            mTvMsg.setTextColor(getContext().getResources().getColor(R.color.common_color));
            mTvButton.setText(R.string.text_cancel);
            mIcon.setBackground(getContext().getResources().getDrawable(R.drawable.shape_circle_blue));
            mIcon.setImageResource(R.mipmap.ic_loading);
        }
        return this;
    }

    public CommonDialog loading() {
        return loading("");
    }

    public CommonDialog error(String msg) {
        if (mTvMsg != null && mTvButton != null && mIcon != null) {
            mTvMsg.setText(msg);
            mTvMsg.setTextColor(getContext().getResources().getColor(R.color.colorFailed));
            mTvButton.setText(R.string.text_confirm);
            mIcon.setBackground(getContext().getResources().getDrawable(R.drawable.shape_circle_red));
            mIcon.setImageResource(R.mipmap.ic_error);
        }
        return this;
    }


    @Override
    public float widthPercent() {
        return 0.78f;
    }

    @Override
    public float heightPercent() {
        return 0.31f;
    }

    private void exit() {
        dismiss();
    }


}